/**
 * 重写createStore，来增强store
 * @param {Function} createStore 创建store
 * @returns store
 */
export default function enhancer(createStore) {
  return function (reducer, preloadedState) {
    // 调用初始的createStore创建store容器
    const store = createStore(reducer, preloadedState);

    const dispatch = store.dispatch;

    // 重写dispatch，加入异步action的处理
    const _dispatch = function (action) {
      if (typeof action === "function") {
        action({ dispatch });
      } else {
        dispatch(action);
      }
    };

    // 返回增强后的store
    return {
      ...store,
      dispatch: _dispatch,
    };
  };
};
